package com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_details;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.GroupDetailsBean;
import com.wangjiyuan.giftshuo.config.classify.ClassifyConstant;
import com.wangjiyuan.giftshuo.ui.fragment.other.detail.DetailPresenter;

import java.util.List;

/**
 * Created by QXL on 2016/11/23.
 *  品类、风格、对象点击item进入的详情画面的item的webView
 */

public class GroupDetWebView extends AppCompatActivity implements View.OnClickListener{
    private Context mContext=this;
    private int position;
    private List<GroupDetailsBean.DataBean.ItemsBean> mList;
    private GroupDetailsBean.DataBean.ItemsBean itemsBean;
    private WebView refreshableView;

    private ImageView back;
    private TextView more;
    private XRecyclerView detail;
    private LinearLayout action_item;
    private TextView favourite;
    private TextView share;
    private TextView comment;
    private int id;
    private ImageView main_image;
    private DetailPresenter presenter;
    private PullToRefreshWebView detail_web_view;
    private LinearLayout linearLayoutWeb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_detail);
        initData();
        initView();
    }

//    加载webView中内容
    private void initContent() {
        itemsBean = mList.get(position);

        refreshableView = detail_web_view.getRefreshableView();
        refreshableView.loadUrl(itemsBean.getUrl());

        refreshableView.getSettings().setJavaScriptEnabled(true);
        refreshableView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        favourite.setText(itemsBean.getLikesCount() + "");
        detail_web_view.onRefreshComplete();
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        position = intent.getIntExtra(ClassifyConstant.LIST_POSITION, 0);
        mList = (List<GroupDetailsBean.DataBean.ItemsBean>) bundle.get(ClassifyConstant.LIST_KEY);
    }

    private void initView() {
        back = (ImageView)findViewById(R.id.back);
        more = (TextView)findViewById(R.id.more);
        action_item = (LinearLayout)findViewById(R.id.action_item);
        favourite = (TextView)findViewById(R.id.favourite);
        share = (TextView)findViewById(R.id.share);
        comment = (TextView)findViewById(R.id.comment);
        detail_web_view = (PullToRefreshWebView) findViewById(R.id.detail_web_view);
        initContent();

        back.setOnClickListener(this);
        more.setOnClickListener(this);
        favourite.setOnClickListener(this);
        share.setOnClickListener(this);
        comment.setOnClickListener(this);

        ILoadingLayout startLabels = detail_web_view.getLoadingLayoutProxy(true, false); //参数1=设置头部，参数2=设置尾部
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        //设置上拉刷新
        ILoadingLayout endLabels = detail_web_view.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        detail_web_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<WebView>() {
//            下拉加载上一页内容
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<WebView> refreshView) {
                if(position==0){
                    Toast.makeText(mContext,"没有更多内容啦",Toast.LENGTH_SHORT).show();
                }
                else {
                    position--;
                    initContent();
                }
                Log.e("webView","pull down==="+position);
                refreshView.onRefreshComplete();
            }

//            上拉加载下一页的内容
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<WebView> refreshView) {
                if(position==mList.size()){
                    Toast.makeText(mContext,"没有更多内容啦",Toast.LENGTH_SHORT).show();
                }
                else {
                    position++;
                    initContent();
                }
                Log.e("webView","pull up==="+position);
                refreshView.onRefreshComplete();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            点击返回
            case R.id.back:
                finish();
                break;
            case R.id.more:
                break;
            case R.id.favourite:
                break;
            case R.id.share:
                break;
            case R.id.comment:
                break;
        }
    }

//    webView资源释放
    @Override
    protected void onDestroy() {
        super.onDestroy();
        refreshableView.stopLoading();
        ((ViewGroup)refreshableView.getParent()).removeView(refreshableView);
        refreshableView.removeAllViews();
        refreshableView.clearCache(true);
        refreshableView.clearHistory();
        refreshableView.destroy();
    }
}
